<?php
class Helper_File
{
	public function chmodr($path, $filemode) {
		if (!is_dir($path)){
			return chmod($path, $filemode);
		}
		$dh = opendir($path);
		while (($file = readdir($dh)) !== false) {
			if($file != '.' && $file != '..') {
				$fullpath = $path.'/'.$file;
				if(is_link($fullpath)){
					return FALSE;
				}
				elseif(!is_dir($fullpath) && !chmod($fullpath, $filemode)){
					return FALSE;
				}
				elseif(!chmodr($fullpath, $filemode)){
					return FALSE;
				}
			}
		}
		closedir($dh);
		if(chmod($path, $filemode)){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	public function createFolder($path)
	{
		$path = rtrim($path,"\\/");

		if ( !file_exists($path) ) {
			if (!file_exists(dirname($path))) {
				$this->createFolder(dirname($path));
			}

			if( mkdir($path, 0777) == false ){
				die($path."Path created failed!");
			}
		}

		chmod($path, 0777);
	}

	public function deleteFolder( $dirName )
	{
		if ( $handle = opendir( $dirName ) ){
			while ( false !== ( $item = readdir( $handle ) ) ) {
				if ( $item != "." && $item != ".." ) {
					if ( is_dir( $dirName."/".$item ) ) {
						$this->deleteFolder( $dirName."/".$item );
					} else {
						unlink( $dirName."/".$item );
					}
				}
			}
			closedir( $handle );
			rmdir( $dirName );
		}
	}

	public function createFile($file, $source , $type)
	{
		if($fp=fopen($file, $type)){

			$filesource=fwrite($fp,$source);
			fclose($fp);
			return true;
		} else {
			return "open failed";
		}
	}

	public function appendContent($path, $content)
	{
		$fp = fopen($path,"a");
		fwrite($fp, $content);
		fclose($fp);
	}

	public function replaceContent($path, $content)
	{
		$fp = fopen($path,"w");
		fwrite($fp, $content);
		fclose($fp);
	}

	public function getFileContent($path)
	{
		if(is_file($path))
		{
			$fp = fopen($path, "r");
			$content = '';

			while (!feof($fp))
			{
				$content .= fgets($fp);
			}

			fclose($fp);
			return $content;
		}
	}
}
// sample of path
//$path = WEBROOT.SEPARATOR.'application'.SEPARATOR.'cache'.SEPARATOR.'presentation'.SEPARATOR.$Username.SEPARATOR.'rating'.SEPARATOR;
?>